<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form
          v-show="showSearch"
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="帖子标题" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入帖子标题"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="摘要" prop="summary">
            <el-input
              v-model="queryParams.summary"
              placeholder="请输入摘要"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>

        <el-table
          v-loading="loading"
          row-key="id"
          :data="articleList"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column prop="id" label="帖子ID" align="center" />
          <el-table-column prop="title" label="标题" align="center" />
          <el-table-column prop="summary" label="摘要" align="center" />
          <el-table-column prop="createTime" label="创建时间" align="center" />
          <el-table-column prop="uid" label="作者编号" align="center" />
          <el-table-column prop="status" label="状态" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status == 0" type="success"
                >审核通过</el-tag
              >
              <el-tag v-if="scope.row.status == 2" type="danger">待审核</el-tag>
            </template>
          </el-table-column>

          <el-table-column prop="isTop" label="是否置顶" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.isTop"
                active-value="1"
                inactive-value="0"
                @change="handleTopChange(scope.row)"
              />
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleVerify(scope.row)"
                >审核</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
      @size-change="getList"
    />
  </div>
</template>

<script>
// import { getToken } from '@/utils/auth'
import {
  listArticle,
  delArticle,
  changeIsTop,
  verifyArticle,
} from "@/api/content/article";

export default {
  name: "Article",
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
      },
      title: "",
      // 是否显示弹出层
      open: false,
      // 总条数
      total: 0,
      articleList: [],
      showSearch: true,
    };
  },
  watch: {},
  created() {
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //置顶状态改变
    handleTopChange(row) {
      const text = row.isTop === "1" ? "置顶" : "取消置顶";
      this.$modal
        .confirm('确认要"' + text + '""' + row.title + '"帖子吗？')
        .then(function () {
          return changeIsTop(row.id, row.isTop);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.isTop = row.isTop === "0" ? "1" : "0";
        });
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then((response) => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 审核按钮操作 */
    handleVerify(row) {
      const text = row.status === "0" ? "打回" : "审核通过";
      this.$modal
        .confirm('确认要"' + text + '""' + row.title + '"帖子吗？')
        .then(function () {
          return verifyArticle(row.id, row.status);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.status = row.status === "0" ? "2" : "0";
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除分类编号为"' + ids + '"的数据项？')
        .then(function () {
          return delArticle(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
